//
//  MerchandiseVC.m
//  1243
//
//  Created by apple on 16/6/30.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "MerchandiseVC.h"
#import "BackGroundView.h"
#import "PicDetailView.h"
#import "CommodityView.h"
#import "JoinDuoBaoView.h"
#import "OrderViewController.h"
@interface MerchandiseVC ()
@property (nonatomic, strong) UISegmentedControl *segment;
@end
@implementation MerchandiseVC
{
    BackGroundView              *BottomView;
    PicDetailView               *picView;
    CommodityView               *detailView;
}
- (UISegmentedControl *)segment {
    if (!_segment) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[@"商品",@"详情",@"评论"]];
        _segment.width = 150;
        _segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor whiteColor]};
        [_segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor lightTextColor]};
        [_segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
        //设置segment初始被选中的下标
        _segment.selectedSegmentIndex = 0;
    }
    return _segment;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.titleView = self.segment;
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(segmentedControlValueChanged) forControlEvents:UIControlEventValueChanged];
    [self creatUI];
    [self changeSegmented];
}
- (void)creatUI {
    BottomView = [[BackGroundView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    BottomView.backgroundColor = [UIColor yellowColor];
    picView = [[PicDetailView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT-64)];
    detailView = [[CommodityView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
#pragma mark ---- 进入状态
    detailView.aititudeNum = 1;
    
    UIButton *joinBT = [UIButton buttonWithType:UIButtonTypeCustom];
    joinBT.frame = CGRectMake(0, HEIGHT - 114, WIDTH, 50);
    joinBT.backgroundColor = [UIColor blueColor];
    [joinBT setTitle:@"参与夺宝" forState:UIControlStateNormal];
    [joinBT addTarget:self action:@selector(touchUpAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:BottomView];
    [BottomView.backGroungView addSubview:picView];
    [BottomView.backGroungView addSubview:detailView];
    [self.view addSubview:joinBT];
}

- (void)changeSegmented {
    __weak typeof(self) __weakSelf = self;
    BottomView.changeScrollBlock = ^void(NSInteger num) {
        if (num == 0) {
            __weakSelf.segment.selectedSegmentIndex = 0;
        }else if (num == 1) {
            __weakSelf.segment.selectedSegmentIndex = 1;
        }else {
            __weakSelf.segment.selectedSegmentIndex = 2;
        }
    };
}

-(void)segmentedControlValueChanged {
    NSInteger index = self.segment.selectedSegmentIndex;
    NSLog(@"%ld",(long)index);
    if (index == 0) {
        [BottomView jumpToIndex:0];
    } else if (index == 1) {
        [BottomView jumpToIndex:1];
    } else if (index == 2) {
        [BottomView jumpToIndex:2];
    }
}

- (void)touchUpAction:(UIButton *)sender {
    JoinDuoBaoView *view = [[JoinDuoBaoView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
#pragma mark   ----   弹出框数据填写
    
    
    view.userInteractionEnabled = YES;
    view.dingdanBlock = ^void(NSString *num,NSString *money,NSString *title,NSString *qi) {
        OrderViewController *vc = [[OrderViewController alloc]init];
        vc.num = num;
        vc.money = money;
        vc.titleName = title;
        vc.qi = qi;
        [self.navigationController pushViewController:vc animated:YES];
    };
    [self.view addSubview:view];
}



@end
